<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
require_once( 'Class_M_OTHER.php' );
class Class_M_COST_FLIER extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table  = "M_COST_FLIER";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey   = "id";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "id";
	 
	 protected $_cols  =  array(
		"shop_cd"      => array(AOKI_VARTYPE_STR),
		"matrix_cd"    => array(AOKI_VARTYPE_INT),
		"plane_cost"   => array(AOKI_VARTYPE_INT)
	 );
	
	 protected $_checks  =  array(
		"shop_cd"        => array( "EXIST" ),
		"matrix_cd"      => array( "EXIST" ),
		"plane_cost"     => array( "EXIST", "NUM" )
	 );
	 
	 //maxlength 
	 protected $_maxlens  = array(
		"shop_cd "        => 3 
	 );
	
	 public function search($where ,&$page = NULL, $matrix_cd,$pre_matrix_cd = '0000'  ){	
	 	$pre_year4 = substr($pre_matrix_cd,0,4);
	 	$year4 = substr($matrix_cd,0,4);
		$sqlFrom  = " from  M_SHOP a ".
			" LEFT JOIN  ( select aa.shop_cd,  " .
			"  sum( if(aa.qtype = '0' , if( cc.mark<>'0',(aa.pnums + aa.sample) * cc.ins_load_ratio ,aa.pnums + aa.sample) * bb.price ,aa.pnums *  pa.price) ) as now_cost  " .
			" from T_MON_CELLD_F  aa  " .
			" left join M_COST_FLIER_PRICE bb on aa.shop_cd = bb.shop_cd and bb.year4={$year4} and aa.size = bb.size " .
			" left join M_COST_FLIER_PRICE_AREA pa on pa.pattern_cd = aa.pattern_cd and pa.year4={$year4} and aa.size = pa.size " .
			" inner join M_MAP_GROUP_SHOP cc on aa.map_no = cc.map_no and aa.matrix_cd = cc.matrix_cd and aa.shop_cd = cc.shop_cd   " .
			" WHERE aa.matrix_cd = '{$matrix_cd}' group by aa.shop_cd )  as b ON b.shop_cd = a.shop_cd  " .
			" inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja " .
			" LEFT JOIN {$this->_table} c on  c.shop_cd = a.shop_cd and c.matrix_cd =  '{$matrix_cd}' " .
			" LEFT JOIN T_SHOP_WEEK  pre on  pre.shop_cd = a.shop_cd and pre.matrix_cd =  '{$pre_matrix_cd}' ";
			
			
		$sql = "SELECT  a.shop_cd, a.shop_name, ua.user_name as area_aja, uz.user_name as zone_aja, c.plane_cost, b.now_cost,pre.cell_cost as last_cost," .
				" (select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name " .
				$sqlFrom ;
				
		$sqlcount = " select count(*) as cnt from  M_SHOP a  inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja " ;
		$order_by = "  a.shop_cd ";
		
		return  parent::search($where, $page, $sql, $sqlcount, $order_by);
	}
	
	 public function searchDM($where ,&$page = NULL, $matrix_cd ,$pre_matrix_cd){
	 	$year4 = substr($matrix_cd,0,4);
		if (empty($pre_matrix_cd)){
			$pre_matrix_cd = 0;
			$pre_year4 =0;
		}else {
			$pre_year4 = substr($pre_matrix_cd,0,4);
		}
		
		$sqlFrom  = " from  M_SHOP a 
		 LEFT JOIN  ( select  a.shop_cd, sum(a.count*pr.price)  as now_cost   from M_COST_DM_COUNT a
		inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
		inner join M_COST_DM_NUMFROM nf on  a.count >= nf.num_from and a.count <=nf.num_to and year4 = {$year4}
		inner join M_COST_DM_PRICE pr on nf.id = pr.nfid and pr.tyid = dm.sort
 		where a.matrix_cd  = {$matrix_cd}
      	group by a.shop_cd)   as b ON b.shop_cd = a.shop_cd 	
	
		LEFT JOIN  ( select  a.shop_cd, sum(a.count*pr.price)  as last_cost   from M_COST_DM_COUNT a
		inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
		inner join M_COST_DM_NUMFROM nf on  a.count >= nf.num_from and a.count <=nf.num_to and year4 = {$pre_year4}
		inner join M_COST_DM_PRICE pr on nf.id = pr.nfid and pr.tyid = dm.sort
 		where a.matrix_cd  = {$pre_matrix_cd}
      	group by a.shop_cd)   as pre  ON pre.shop_cd = a.shop_cd 
		 LEFT JOIN {$this->_table} c on  c.shop_cd = a.shop_cd and c.matrix_cd =  '{$matrix_cd}' 
		 LEFT JOIN M_SHOP_RHIZOME  r on  r.shop_cd = a.shop_cd and r.matrix_cd =  '{$matrix_cd}'" .
		 " inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja "  ;
	
		$sql = "SELECT  a.shop_cd, a.shop_name,ua.user_name as area_aja, uz.user_name as zone_aja,c.plane_dm as plane_cost, b.now_cost, pre.last_cost," .
				" (select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name " .
				$sqlFrom ;
		$sqlcount = " select count(*) as cnt from  M_SHOP a inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja " ;
		$order_by = "  a.shop_cd ";
		return  parent::search($where, $page, $sql, $sqlcount, $order_by);
	}
	
 public function searchQua($where ,&$page = NULL, $matrix_cd ,$pre_matrix_cd){
	 	/*
	 	$year4 = substr($matrix_cd,0,4);
		if (empty($pre_matrix_cd)){
			$pre_matrix_cd = 0;
			$pre_year4 =0;
		}else {
			$pre_year4 = substr($pre_matrix_cd,0,4);
		}
		*/
		
		$sqlFrom  = " from  M_SHOP a 
		 LEFT JOIN {$this->_table} c on  c.shop_cd = a.shop_cd and c.matrix_cd =  '{$matrix_cd}' 
		 LEFT JOIN M_SHOP_RHIZOME  r on  r.shop_cd = a.shop_cd and r.matrix_cd =  '{$matrix_cd}'" .
		 		" inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja" ;
		
		$sql = "SELECT  a.shop_cd, a.shop_name, ua.user_name as area_aja, uz.user_name as zone_aja, c.plane_qua as plane_cost, r.now_quantity as now_cost, r.last_quantity as last_cost" .
				$sqlFrom  ;
		
		$sqlcount = " select count(*) as cnt from  M_SHOP a inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja " ;
		$order_by = "  a.shop_cd ";
		
		return  parent::search($where, $page, $sql, $sqlcount, $order_by);
	}
	
	public function searchList(&$page , $fmsort ,$fminit,$fmformat ,$year4, $matrix_cd =NULL, $month = NULL, $aja =NULL){
	 	
	 	$last_year4 = $year4 -1;
	 	$matrixFields = "";
	 	if  ( $fmformat == '4' ){
	 		
	 		$matrixFields = ",left(flier_date, 6) as month";
	 	}
	 	if  ( $fmformat == '5' ){
	 		
	 		$matrixFields = ",week_cd,week_seq,flier_date";
	 	}
	 	
	 	$tableMatrix = " select matrix_cd,year4,pre_matrix_cd{$matrixFields} from M_MATRIX_TERM where year4={$year4} ";
	 	if (!empty($matrix_cd)){
	 		$tableMatrix =$tableMatrix. " and matrix_cd = {$matrix_cd}";
	 	}
	 
	 	if (!empty($month)){
	 		$realyear4 = $year4;
	 		if ( $month<=3 ){
	 			$realyear4 = $realyear4 + 1;
	 		}
	 		$smonth = $month;
	 		if ( $month<=10 ){
	 			$smonth = '0'.$month;
	 		}
	 		$from_date = $year4.$smonth."00" ;
	 		$to_date   = $year4.$smonth."31" ;
	 		$tableMatrix =$tableMatrix. " and flier_date >= {$from_date} and  flier_date <= {$to_date} ";
	 	}
	 	$joinQua  = " LEFT JOIN M_SHOP_RHIZOME  qua on  qua.shop_cd = a.shop_cd and qua.matrix_cd =  mst.matrix_cd	" ;
	 	
		//'area_aja'         => array( 'エリアAJA名',  1),
		//'zone_aja'         => array( 'ゾーンAJA名',  1)
	 	//$fmformat = 1 店舗単位 2 エリア 3ゾーン  4 月単位 5 週単位
	 	$sqlcount = " select count(distinct a.shop_cd) as cnt from  M_SHOP a  inner join ( {$tableMatrix} ) mst " ;
	 	$joinSqlAja = "";
		switch ($fmformat) {
	    case 1:
	 		$order_by = "  a.shop_cd ";
	 		$group_by = "  a.shop_cd ";
	 		$fromName = "a.shop_cd, a.shop_name,ua.user_name as area_aja , uz.user_name as zone_aja,";
	 		$joinAja = " inner join M_LOGIN ua on ua.id = a.area_aja inner join   M_LOGIN uz on uz.id = a.zone_aja ";
	        break;
	    case 2:
	 		$order_by = "  a.area_aja ";
	 		$group_by = "  a.area_aja";
	 		$fromName = " ua.login_id as aja_cd,ua.user_name as aja_name, ";
	 		$joinAja = " inner join M_LOGIN ua on ua.id = a.area_aja  ";
	 		$sqlcount = " select count(distinct a.area_aja) as cnt from  M_SHOP a  inner join ( {$tableMatrix} ) mst  " ;
	        break;
	    case 3:
	 		$order_by = "  a.zone_aja ";
	 		$group_by = "  a.zone_aja";
	 		$fromName = " za.login_id as aja_cd,za.user_name as aja_name, ";
	 		$joinAja = " inner join M_LOGIN za on za.id = a.zone_aja  ";
	 		$sqlcount = " select count(distinct a.zone_aja) as cnt from  M_SHOP a  inner join ( {$tableMatrix} ) mst  " ;
	        break;
	    case 4:
	 		$order_by = "  mst.month ";
	 		$group_by = "  mst.month ";
	 		$fromName = "  mst.month,";
	 		$sqlcount = " select count(distinct mst.month) as cnt from  M_SHOP a  inner join ( {$tableMatrix} ) mst " ;
	        break;
	    case 5:
	 		$order_by = "  mst.matrix_cd ";
	 		$group_by = "  mst.matrix_cd ";
	 		$fromName =   "mst.matrix_cd, mst.week_cd,mst.week_seq,mst.flier_date,";
	 		$sqlcount = " select count(distinct mst.matrix_cd) as cnt from  M_SHOP a  inner join ( {$tableMatrix} ) mst " ;
	        break;

	     default:
	 		$order_by = "  a.shop_cd ";
	 		$group_by = "  a.shop_cd ";
	 		$fromName = "a.shop_cd, a.shop_name,";
	        break;
		}
	 	$colFLI = "";
	 	$colDM = "";
	 	
	 	if ($fmsort == '1') {
	 		$joinDM = "";
	 	}else{
	 		$colDM = ",sum(c.plane_dm) as plane_dm , sum(dm.now_dm) as now_dm, sum(pdm.last_dm) as last_dm";
	 		
			$joinDM  = "  
			 LEFT JOIN  ( 	select  a.matrix_cd,a.shop_cd, a.count*pr.price  as now_dm   from M_COST_DM_COUNT a
			inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
			inner join M_COST_DM_NUMFROM nf on  a.count >= nf.num_from and a.count <=nf.num_to and year4 = {$year4}
			inner join M_COST_DM_PRICE pr on nf.id = pr.nfid and pr.tyid = dm.sort
	 		where left(a.matrix_cd,4)  =  {$year4})   as  dm  ON dm.shop_cd = a.shop_cd  and dm.matrix_cd = mst.matrix_cd	
			LEFT JOIN  ( select  a.matrix_cd, a.shop_cd, a.count*pr.price  as last_dm   from M_COST_DM_COUNT a
			inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
			inner join M_COST_DM_NUMFROM nf on  a.count >= nf.num_from and a.count <=nf.num_to and year4 = {$last_year4}
			inner join M_COST_DM_PRICE pr on nf.id = pr.nfid and pr.tyid = dm.sort
	 		where left(a.matrix_cd,4)  = {$last_year4})   as pdm  ON pdm.shop_cd = a.shop_cd and pdm.matrix_cd = mst.matrix_cd	
			 " ;
	 	}
		if ($fmsort == '2') {
	 		$joinFLI = "";
	 	}else{
	 		$colFLI = ",sum(c.plane_cost ) as plane_cost , sum(fli.now_cost ) as now_cost,sum(pre.cell_cost  ) as last_cost ";
	 		
			$joinFLI  = "  
			 LEFT JOIN   
			  ( select aa.shop_cd,aa.matrix_cd , 
			  sum( if(aa.qtype = '0' , if( cc.mark<>'0',(aa.pnums + aa.sample) * cc.ins_load_ratio ,aa.pnums + aa.sample) * bb.price ,aa.pnums *  pa.price))  as now_cost  
			  from T_MON_CELLD_F  aa  
			  left join M_COST_FLIER_PRICE bb on aa.shop_cd = bb.shop_cd and bb.year4={$year4} and aa.size = bb.size 
			  left join M_COST_FLIER_PRICE_AREA pa on pa.pattern_cd = aa.pattern_cd and pa.year4={$year4} and aa.size = pa.size 
			  inner join M_MAP_GROUP_SHOP cc on aa.map_no = cc.map_no and aa.matrix_cd = cc.matrix_cd and aa.shop_cd = cc.shop_cd  
			  WHERE left(aa.matrix_cd,4)  =  {$year4} group by aa.shop_cd,aa.matrix_cd  )  as fli
			  on fli.shop_cd = a.shop_cd and fli.matrix_cd = mst.matrix_cd ";  
		}
		
		$sqlcount = $sqlcount.$joinAja;
		$where = array();
		$whereaja = "";
		if (!empty($aja)){
			$whereaja = " where   (a.zone_aja = {$aja} or a.area_aja = {$aja})  ";
			$sqlcount = $sqlcount.$whereaja ;
		} 
		$sql = "SELECT  {$fromName}" .
		   "sum(c.plane_qua) as plane_qua , sum(qua.now_quantity) as now_qua, sum(qua.last_quantity) as last_qua" .$colFLI.$colDM.
			" from  M_SHOP a   " .
			" inner join ( {$tableMatrix} ) mst " .
			" LEFT JOIN {$this->_table} c on  c.shop_cd = a.shop_cd and c.matrix_cd =  mst.matrix_cd " .
			" LEFT JOIN T_SHOP_WEEK  pre on  pre.shop_cd = a.shop_cd and pre.matrix_cd =  mst.pre_matrix_cd  " .
			$joinAja.$joinDM. $joinFLI.$joinQua.$whereaja." group by ".$group_by;
		
		return   parent::search($where, $page, $sql, $sqlcount, $order_by);
		//print_r($dataa);
	 }
	 
	public function get_valuesDM($shop_cd, $matrix_cd,$pre_matrix_cd ) {
		$year4 = substr($matrix_cd,0,4);
		if (empty($pre_matrix_cd)){
			$pre_matrix_cd = 0;
			$pre_year4 =0;
		}else {
			$pre_year4 = substr($pre_matrix_cd,0,4);
		}
		$sql ="SELECT s.shop_cd,s.shop_name,a.plane_dm as plane_cost,a.id, a.u_time,
		(select   sum(a.count*pr.price)  as now_cost   from M_COST_DM_COUNT a
		inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
		inner join M_COST_DM_NUMFROM nf on  a.count >= nf.num_from and a.count <=nf.num_to and year4 = {$year4}
		inner join M_COST_DM_PRICE pr on nf.id = pr.nfid and pr.tyid = dm.sort
 		where a.matrix_cd  = {$matrix_cd} and a.shop_cd ='{$shop_cd}' ) as now_cost, 
		( select  sum(a.count*pr.price)  as last_cost   from M_COST_DM_COUNT a
		inner join  M_COST_DM dm on a.matrix_cd = dm.matrix_cd and a.dm_no = dm.dm_no 
		inner join M_COST_DM_NUMFROM nf on  a.count >= nf.num_from and a.count <=nf.num_to and year4 = {$pre_year4}
		inner join M_COST_DM_PRICE pr on nf.id = pr.nfid and pr.tyid = dm.sort
 		where a.matrix_cd  = {$pre_matrix_cd} and  a.shop_cd ='{$shop_cd}' ) as last_cost ,
				 (select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name  
		from M_SHOP s 
		left join  {$this->_table} a on a.shop_cd = s.shop_cd and  a.matrix_cd = '{$matrix_cd}' 
		WHERE s.shop_cd = '{$shop_cd}' " ;
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$retrow =  $this->_db->fetch();
			if (isset($retrow["u_user"]) && ($retrow["u_user"])) {
				$sql = sprintf("SELECT user_name FROM  M_LOGIN  WHERE id = '%s'" , $retrow["u_user"]);
				$this->_db->query( $sql );
				if( $this->_db->num_rows() ){
					$userrow = $this->_db->fetch();
					$retrow["m_user_name"] = $userrow['user_name'];
				}else {
					$retrow["m_user_name"] = $retrow["u_user"];
				}
			}
			return $retrow;
		}else{
			return  FALSE;
		}
	}
	
	public function get_valuesQua($shop_cd, $matrix_cd,$pre_matrix_cd ) {
		$year4 = substr($matrix_cd,0,4);
		if (empty($pre_matrix_cd)){
			$pre_matrix_cd = 0;
			$pre_year4 =0;
		}else {
			$pre_year4 = substr($pre_matrix_cd,0,4);
		}
		
		$sql ="SELECT s.shop_cd,s.shop_name,a.plane_qua as plane_cost,a.id, a.u_time,r.now_quantity as now_cost, r.last_quantity as last_cost,
				 (select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name  
		from M_SHOP s  
		left join  {$this->_table} a on a.shop_cd = s.shop_cd and  a.matrix_cd = '{$matrix_cd}' 
		LEFT JOIN M_SHOP_RHIZOME  r on  r.shop_cd = s.shop_cd and r.matrix_cd =  '{$matrix_cd}'
		WHERE s.shop_cd = '{$shop_cd}' " ;
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$retrow =  $this->_db->fetch();
			if (isset($retrow["u_user"]) && ($retrow["u_user"])) {
				$sql = sprintf("SELECT user_name FROM  M_LOGIN  WHERE id = '%s'" , $retrow["u_user"]);
				$this->_db->query( $sql );
				if( $this->_db->num_rows() ){
					$userrow = $this->_db->fetch();
					$retrow["m_user_name"] = $userrow['user_name'];
				}else {
					$retrow["m_user_name"] = $retrow["u_user"];
				}
			}
			return $retrow;
		}else{
			return  FALSE;
		}
	}
	
	public function get_values($shop_cd, $matrix_cd,$pre_matrix_cd ) {
				
		$year4 = substr($matrix_cd,0,4);
		if (empty($pre_matrix_cd)){
			$pre_matrix_cd = 0;
			$pre_year4 =0;
		}else {
			$pre_year4 = substr($pre_matrix_cd,0,4);
		}
		$sql ="SELECT s.shop_cd,s.shop_name,a.plane_cost,a.id, a.u_time,
		(select  sum( if(aa.qtype = '0' , if( cc.mark<>'0',(aa.pnums + aa.sample) * cc.ins_load_ratio ,aa.pnums + aa.sample) * bb.price ,
		aa.pnums * pa.price) ) as now_cost 
		from T_MON_CELLD_F aa left join M_COST_FLIER_PRICE bb on aa.shop_cd = bb.shop_cd and bb.year4={$year4} and aa.size = bb.size 
		left join M_COST_FLIER_PRICE_AREA pa on pa.pattern_cd = aa.pattern_cd and pa.year4={$year4} and aa.size = pa.size
 		inner join M_MAP_GROUP_SHOP cc on aa.map_no = cc.map_no and aa.matrix_cd = cc.matrix_cd and aa.shop_cd = cc.shop_cd 
 		WHERE aa.matrix_cd = $matrix_cd  and aa.shop_cd = '{$shop_cd}' ) as now_cost,  
		( select cell_cost from T_SHOP_WEEK where matrix_cd = '{$pre_matrix_cd}' and shop_cd ='{$shop_cd}' ) as last_cost,
				 (select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name  
		from M_SHOP s 
		left join  {$this->_table} a on a.shop_cd = s.shop_cd and  a.matrix_cd = '{$matrix_cd}' 
		WHERE s.shop_cd = '{$shop_cd}' " ;
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$retrow =  $this->_db->fetch();
			if (isset($retrow["u_user"]) && ($retrow["u_user"])) {
				$sql = sprintf("SELECT user_name FROM  M_LOGIN  WHERE id = '%s'" , $retrow["u_user"]);
				$this->_db->query( $sql );
				if( $this->_db->num_rows() ){
					$userrow = $this->_db->fetch();
					$retrow["m_user_name"] = $userrow['user_name'];
				}else {
					$retrow["m_user_name"] = $retrow["u_user"];
				}
			}
			return $retrow;
		}else{
			return  FALSE;
		}
	}
public function  get_txt_data($where, $year4){	
		//$sql = " select * from *M_COST_DM_PRICE* where   year4 =  $year4 ";
		//$year4 = substr($matrix_cd,0,4);
		/*
		$sql = "SELECT a.shop_cd,substring(b.flier_date,5,2)  as month," .
				" sum(a.pnums) as money " .
				"  from T_MON_CELLD_F a " .
				"inner  join M_MATRIX_TERM b on a.matrix_cd = b.matrix_cd ";
		//SUBSTRING(str,pos,len)
		$sql = $sql." where ".implode(' AND ', $where) ;
		$sql = $sql." group by month,a.shop_cd order by month,a.shop_cd  ";
		*/
		$sql  = " select aa.shop_cd,substring(b.flier_date,5,2)  as month,  " .
			" sum( if(aa.qtype = '0' , if( cc.mark<>'0',(aa.pnums+aa.sample) * cc.ins_load_ratio/100 ,aa.pnums+aa.sample) * bb.price ,aa.pnums *  pa.price) ) as money  " .
			" from  T_MON_CELLD_F  aa  " .
			" left join M_COST_FLIER_PRICE bb on aa.shop_cd = bb.shop_cd and bb.year4={$year4} and aa.size = bb.size " .
			" left join M_COST_FLIER_PRICE_AREA pa on pa.pattern_cd = aa.pattern_cd and pa.year4={$year4} and aa.size = pa.size " .
			" inner join M_MATRIX_TERM b on aa.matrix_cd = b.matrix_cd   " .
			" inner join M_MAP_GROUP_SHOP cc on aa.map_no = cc.map_no and aa.matrix_cd = cc.matrix_cd and aa.shop_cd = cc.shop_cd  " .
			" where ".implode(' AND ', $where)  .
			" group by  aa.matrix_cd,aa.shop_cd  order by aa.matrix_cd,aa.shop_cd  " ;
		try {
			$limit = "";
		
			$this->_db->query( $sql );
			
			$data = array();
			while( $row = $this->_db->fetch() ){
				$data[] = $row;
			}
			return $data;
		} catch (Exception $e) {
			echo "例外キャッチ：", $e->getMessage(), "\n";
			return NULL;
		}
	}
}

?>